//------------------------------------------------------------------------------
//  defaulthttprequesthandler.cc
//  (C) 2007 Radon Labs GmbH
//------------------------------------------------------------------------------
#include "stdneb.h"
#include "core/coreserver.h"
#include "http/defaulthttprequesthandler.h"
#include "timing/calendartime.h"
#include "http/htmlpagewriter.h"
#include "http/httpserver.h"

namespace Http
{
ImplementClass(Http::DefaultHttpRequestHandler, 'DHRH', Http::HttpRequestHandler);

using namespace Core;
using namespace Util;
using namespace Timing;

//------------------------------------------------------------------------------
/**
*/
HttpStatus::Code
DefaultHttpRequestHandler::HandleRequest(HttpMethod::Code requestMethod, const IO::URI& requestUri, const Ptr<IO::Stream>& responseContentStream)
{
    // don't accept anything then Get
    if (HttpMethod::Get != requestMethod)
    {
        return HttpStatus::NotImplemented;
    }
    
    // always show the home page
    Ptr<HtmlPageWriter> htmlWriter = HtmlPageWriter::Create();
    htmlWriter->SetStream(responseContentStream);
    htmlWriter->SetTitle("Nebula3 Application");
    if (htmlWriter->Open())
    {
        htmlWriter->Element(HtmlElement::Heading1, "Main Page");
        htmlWriter->Element(HtmlElement::Heading3, "Application Info");
        htmlWriter->Begin(HtmlElement::Table);
            htmlWriter->Begin(HtmlElement::TableRow);
                htmlWriter->Element(HtmlElement::TableData, "Company Name:");
                htmlWriter->Element(HtmlElement::TableData, CoreServer::Instance()->GetCompanyName());
            htmlWriter->End(HtmlElement::TableRow);
            htmlWriter->Begin(HtmlElement::TableRow);
                htmlWriter->Element(HtmlElement::TableData, "Application Name:");
                htmlWriter->Element(HtmlElement::TableData, CoreServer::Instance()->GetAppName());
            htmlWriter->End(HtmlElement::TableRow);
            htmlWriter->Begin(HtmlElement::TableRow);            
                htmlWriter->Element(HtmlElement::TableData, "Calendar Time:");
                htmlWriter->Element(HtmlElement::TableData, CalendarTime::Format("{WEEKDAY} {DAY}.{MONTH}.{YEAR} {HOUR}:{MINUTE}:{SECOND}", CalendarTime::GetLocalTime()));
            htmlWriter->End(HtmlElement::TableRow);
        htmlWriter->End(HtmlElement::Table);

        // list registered HttpRequestHandlers
        htmlWriter->Element(HtmlElement::Heading3, "Available Pages");        
        const Array<Ptr<HttpRequestHandler>>& handlers = HttpServer::Instance()->GetRequestHandlers();
        if (handlers.Size() > 0)
        {
            htmlWriter->Begin(HtmlElement::UnorderedList);
            IndexT i;
            for (i = 0; i < handlers.Size(); i++)
            {
                const Ptr<HttpRequestHandler>& handler = handlers[i];
                htmlWriter->Begin(HtmlElement::ListItem);
                htmlWriter->AddAttr("href", handler->GetRootLocation());
                htmlWriter->Element(HtmlElement::Anchor, handler->GetName());
                htmlWriter->Text(String(" - ") + handler->GetDesc());
                htmlWriter->End(HtmlElement::ListItem);
            }
            htmlWriter->End(HtmlElement::UnorderedList);
        }
        htmlWriter->Close();
        return HttpStatus::OK;
    }
    return HttpStatus::InternalServerError;
}

} // namespace Http